#include"login_ui.h"
#include"General_func.h"
#include"admin_ui.h"
#include"value.h"
#include"user_ui.h"
#include"log_system.h"
#include<string.h>
#include<stdlib.h>
#include"../lv_chinese_ime/lv_chinese_ime.h"


// LV_FONT_DECLARE(lv_font_source_han_sans_bold_16);

void cur_login_user_info(User *tmp)
{
    strcpy(cur_user.username,tmp->username);
    strcpy(cur_user.password,tmp->password);
    cur_user.identity = tmp->identity;
    cur_user.discount = tmp->discount;
    cur_user.credits = tmp->credits;
    cur_user.balance = tmp->balance;
    cur_user.Total_consumption = tmp->Total_consumption;
}

/**
 * 返回最初身份选择页面
*/
void return_origin_event_cb(lv_event_t *e)
{
    free(goods_list);
    goods_list=NULL;
    log_add(LOG_LOGOUT,cur_user.username,cur_user.identity,"LOG OUT");
    log_save_text("log.txt");
    Identity_choice();
}

/**
 * 消息框响应
*/
void event_cb_msgbox(lv_event_t *e)
{
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t *target = lv_event_get_current_target(e);
    if(code == LV_EVENT_PRESSED)
    {
        if(lv_msgbox_get_active_btn(target) == 2)    /* 获取按钮索引 */
            lv_obj_add_flag(target, LV_OBJ_FLAG_HIDDEN);  /* 添加隐藏标志 */
    }
}

//登录按钮的回调
void login_btn_event_cb(lv_event_t *e)
{
    lv_obj_t *target = lv_event_get_target(e);
    seleteData *selete_data = (UserData*)lv_event_get_user_data(e);
    if(selete_data == NULL)
    {
        printf("Error:selete_data is NULL\n");
        return;
    }
    char *username = lv_textarea_get_text(selete_data->username_ta);
    char *password = lv_textarea_get_text(selete_data->password_ta);
    // printf("username = %s\n",username);
    // printf("password = %s\n",password);

    if(strlen(username) == 0 || strlen(password)==0)
    {
        lv_obj_t * mbox = lv_msgbox_create(NULL, "error", "input not null", NULL, true);
        lv_obj_center(mbox);
        return;
    }
    char flag[32];
    strcpy(flag,selete_data->flag);

    char username1[32];
    char password1[32];
    int authority=0;
    int login_flag = 0;
    User *tmp = user_find_node_by_username(user_list,username);
    // printf("tmp->name = %s\n",tmp->username);
    // printf("user_list->next->name = %s\n",user_list->next->username);
    if(tmp != NULL)
    {
        if(strcmp(tmp->password,password) == 0)
        {
            if((strcmp(flag,"Admin login") ==0) && (tmp->identity == 0))
            {
                // printf("amdin login success\n");
                login_flag=1;
                cur_login_user_info(tmp);
                log_add(LOG_LOGOUT,cur_user.username,cur_user.identity,"Login successful");
                admin_ui_init();
            }
            else if((strcmp(flag,"User login") ==0) && (tmp->identity > 0))
            {
                // printf("user login success\n");
                login_flag=1;
                cur_login_user_info(tmp);
                log_add(LOG_LOGOUT,cur_user.username,cur_user.identity,"Login successful");
                user_ui_init();
            }
        }
    }
    //设置弹窗
    if(login_flag == 0)
    {
        static const char *msgbtn[] = {" "," ","OK",""};
        lv_obj_t *msgbox = lv_msgbox_create(lv_scr_act(),LV_SYMBOL_WARNING"Notice"," Incorrect input of account or password.",msgbtn,true);
        lv_obj_set_size(msgbox,350,130);
        lv_obj_align(msgbox,LV_ALIGN_CENTER,0,0);
        lv_obj_t* btn = lv_msgbox_get_btns(msgbox);
        lv_obj_set_style_bg_opa(btn, 0, LV_PART_ITEMS);    //背景透明
        lv_obj_set_style_shadow_width(btn, 0, LV_PART_ITEMS);    //去除阴影
        lv_obj_set_style_text_color(btn, lv_color_hex(0x0000ff),LV_PART_ITEMS);    //字体颜色
        lv_obj_add_event_cb(msgbox, event_cb_msgbox, LV_EVENT_PRESSED,NULL);
    }
}

//注册的回调
void register_event_cb(lv_event_t *e)
{
    lv_obj_t *target = lv_event_get_target(e);
    UserData *user_data = (UserData*)lv_event_get_user_data(e);
    if(user_data == NULL)
    {
        printf("Error:user_data is NULL\n");
        return;
    }
    const char *username = lv_textarea_get_text(user_data->username_ta);
    char *password = lv_textarea_get_text(user_data->password_ta);
    char *password_twice = lv_textarea_get_text(user_data->password_ta2);
    int authority=0;
    char username1[32];
    char password1[32];
    int authority1=0;
    static const char *msgbtn[] = {" "," ","OK",""};
    
    User *tmp = user_find_node_by_username(user_list,username);
    if(tmp != NULL)
    {
        printf("The account already exists.\n");
            lv_obj_t *msgbox = lv_msgbox_create(lv_scr_act(),LV_SYMBOL_WARNING"Notice"," The account already exists.",msgbtn,true);
            lv_obj_set_size(msgbox,350,130);
            lv_obj_align(msgbox,LV_ALIGN_CENTER,0,0);
            lv_obj_t* btn = lv_msgbox_get_btns(msgbox);
            lv_obj_set_style_bg_opa(btn, 0, LV_PART_ITEMS);    //背景透明
            lv_obj_set_style_shadow_width(btn, 0, LV_PART_ITEMS);    //去除阴影
            lv_obj_set_style_text_color(btn, lv_color_hex(0x0000ff),LV_PART_ITEMS);    //字体颜色
            lv_obj_add_event_cb(msgbox, event_cb_msgbox, LV_EVENT_PRESSED,NULL);
            return ;
    }


    if(strcmp(password,password_twice) == 0)
    {
        Userinfo user= {
            .identity= 1,
            .Total_consumption = 0,
            .discount = 1,
            .credits = 0,
            .balance = 0
        };
        strcpy(user.username,username);
        strcpy(user.password,password);
        printf("username=%s\n",user.username);
        printf("password=%s\n",user.password);
        User *new = user_new_node(user);
        user_insert_node(user_list,new);
        printf("register success\n");
        user_file_save_data(user_list,"user.txt");
        lv_obj_t *msgbox = lv_msgbox_create(lv_scr_act(),LV_SYMBOL_WARNING"Notice"," register success.\n 2 seconds later, automatically return to the previous interface. ",msgbtn,true);
        lv_obj_set_size(msgbox,350,150);
        lv_obj_align(msgbox,LV_ALIGN_CENTER,0,0);
        lv_obj_t* btn = lv_msgbox_get_btns(msgbox);
        lv_obj_set_style_bg_opa(btn, 0, LV_PART_ITEMS);    //背景透明
        lv_obj_set_style_shadow_width(btn, 0, LV_PART_ITEMS);    //去除阴影
        lv_obj_set_style_text_color(btn, lv_color_hex(0x0000ff),LV_PART_ITEMS);    //字体颜色
        Identity_choice();
    }else{
        printf("twice password error\n");
        lv_obj_t *msgbox = lv_msgbox_create(lv_scr_act(),LV_SYMBOL_WARNING"Notice","The second password is inconsistent with the first one.",msgbtn,true);
        lv_obj_set_size(msgbox,350,150);
        lv_obj_align(msgbox,LV_ALIGN_CENTER,0,0);
        lv_obj_t* btn = lv_msgbox_get_btns(msgbox);
        lv_obj_set_style_bg_opa(btn, 0, LV_PART_ITEMS);    //背景透明
        lv_obj_set_style_shadow_width(btn, 0, LV_PART_ITEMS);    //去除阴影
        lv_obj_set_style_text_color(btn, lv_color_hex(0x0000ff),LV_PART_ITEMS);    //字体颜色
        lv_obj_add_event_cb(msgbox, event_cb_msgbox, LV_EVENT_PRESSED,NULL);
    }

}

//注册界面
void register_ui_event_cb(lv_event_t *e)
{

    lv_obj_t *target = lv_event_get_target(e);

    /*------1.注册界面------*/
    lv_obj_t *register_screen = lv_obj_create(lv_scr_act());
    lv_obj_set_size(register_screen,800,480);
    lv_obj_set_style_border_width(register_screen, 0, 0);
    lv_obj_set_style_pad_all(register_screen, 0, 0);
    /*------2.背景图片------*/
    lv_obj_t *Login_bg_img = lv_img_create(register_screen);
    //    设置图片源
    LV_IMG_DECLARE(login_bg);
    lv_img_set_src(Login_bg_img,&login_bg);
    lv_obj_set_size(Login_bg_img,800,480);

    /*------3.文本框:账号、密码、再次输入密码-----*/
    //    3.1账号
    lv_obj_t *username_ta = lv_textarea_create(register_screen);
    lv_obj_set_size(username_ta, 300, 40);
    lv_obj_align(username_ta, LV_ALIGN_TOP_MID, 0, 50);
    lv_obj_remove_style(username_ta, NULL, LV_PART_SCROLLBAR);    //移除滚动条样式
    lv_textarea_set_placeholder_text(username_ta, "username");//占位符的内容
    lv_obj_add_event_cb(username_ta, keyboard_event_cb, LV_EVENT_ALL,NULL);//键盘
    //   3.2密码
    lv_obj_t *password_ta = lv_textarea_create(register_screen);
    lv_obj_set_size(password_ta, 300, 40);
    lv_obj_align_to(password_ta,username_ta,LV_ALIGN_OUT_BOTTOM_MID, 0, 40);
    lv_obj_remove_style(password_ta, NULL, LV_PART_SCROLLBAR);
    lv_textarea_set_password_mode(password_ta, true);//设置密码模式，输入以*代替
    lv_textarea_set_placeholder_text(password_ta, "password");
    lv_obj_add_event_cb(password_ta, keyboard_event_cb, LV_EVENT_ALL,NULL);

    //   3.3 再次输入密码
    lv_obj_t *password_ta2 = lv_textarea_create(register_screen);
    lv_obj_set_size(password_ta2, 300, 40);
    lv_obj_align_to(password_ta2,password_ta,LV_ALIGN_OUT_BOTTOM_MID, 0, 40);
    lv_obj_remove_style(password_ta2, NULL, LV_PART_SCROLLBAR);
    lv_textarea_set_password_mode(password_ta2, true);//设置密码模式，输入以*代替
    lv_textarea_set_placeholder_text(password_ta2, "twice password");
    lv_obj_add_event_cb(password_ta2, keyboard_event_cb, LV_EVENT_ALL,NULL);

    //  3.4创建user标签
    lv_obj_t *label_user = lv_label_create(register_screen);
    lv_label_set_text(label_user, "");
    lv_obj_set_style_text_font(label_user, &lv_font_montserrat_20, LV_PART_MAIN);
    lv_obj_align_to(label_user,username_ta, LV_ALIGN_OUT_RIGHT_MID,30,0);
    //  3.5创建password标签
    lv_obj_t *label_password = lv_label_create(register_screen);
    lv_label_set_text(label_password, "");
    lv_obj_set_style_text_font(label_password, &lv_font_montserrat_20, LV_PART_MAIN);
    lv_obj_align_to(label_password,password_ta, LV_ALIGN_OUT_RIGHT_MID,30,0);

    // 4.注册按钮
    lv_obj_t *Register_btn = lv_btn_create(register_screen);
    lv_obj_set_size(Register_btn,100,50);
    lv_obj_align_to(Register_btn,password_ta2, LV_ALIGN_OUT_BOTTOM_MID,0,40);

    lv_obj_t *Register_label = lv_label_create(Register_btn);
    lv_obj_center(Register_label);
    lv_label_set_text(Register_label,"Register");
    lv_obj_set_style_text_font(Register_label,&lv_font_montserrat_20,0);

    UserData *user_data = malloc(sizeof(UserData));
    if (user_data == NULL) {
        printf("Memory allocation failed\n");
        return;
    }
    user_data->username_ta = username_ta;
    user_data->password_ta = password_ta;
    user_data->password_ta2 = password_ta2;
    lv_obj_add_event_cb(Register_btn,register_event_cb,LV_EVENT_CLICKED,(void *)user_data);

    //键盘
    keyboard = lv_keyboard_create(lv_scr_act());
    lv_keyboard_set_textarea(keyboard, NULL);
    lv_obj_add_flag(keyboard, LV_OBJ_FLAG_HIDDEN);

    //5.退出
    lv_obj_t *quit_btn = lv_btn_create(register_screen);
    lv_obj_set_size(quit_btn,80,50);
    lv_obj_t *quit_label = lv_label_create(quit_btn);

    lv_obj_center(quit_label);
    lv_label_set_text(quit_label,LV_SYMBOL_LEFT);
    lv_obj_set_style_text_font(quit_label,&lv_font_montserrat_20,0);
    
    lv_obj_add_event_cb(quit_btn,Login_interface_cb,LV_EVENT_CLICKED,quit_btn);
}

//登录界面
void Login_interface_cb(lv_event_t *e)
{
    lv_obj_t *target = lv_event_get_target(e);
    lv_obj_t *label;
    char buf[30];
    lv_obj_t *data = lv_event_get_user_data(e);
    if(data == NULL)
    {
        label = lv_obj_get_child(target,0);
        strcpy(buf,lv_label_get_text(label));
    }else{
        strcpy(buf,"User login");
    }
    /*-----1.登录界面-----*/
    lv_obj_t *login_screen = lv_obj_create(lv_scr_act());
    lv_obj_set_size(login_screen,800,480);
    lv_obj_set_style_border_width(login_screen, 0, 0);
    lv_obj_set_style_pad_all(login_screen, 0, 0);

    /*------2.背景图片------*/
    lv_obj_t *Login_bg_img = lv_img_create(login_screen);
    //    设置图片源
    LV_IMG_DECLARE(login_bg);
    lv_img_set_src(Login_bg_img,&login_bg);
    lv_obj_set_size(Login_bg_img,800,480);

    /*-------3.文本框(账号密码)--------*/
    //    3.1账号
    lv_obj_t *username_ta = lv_textarea_create(login_screen);
    lv_obj_set_size(username_ta, 300, 40);
    lv_obj_align(username_ta, LV_ALIGN_TOP_MID, 0, 50);
    lv_obj_remove_style(username_ta, NULL, LV_PART_SCROLLBAR);    //移除滚动条样式
    lv_textarea_set_placeholder_text(username_ta, "username");//占位符的内容
    lv_obj_add_event_cb(username_ta, keyboard_event_cb, LV_EVENT_ALL,NULL);//键盘
    //   3.2密码
    lv_obj_t *password_ta = lv_textarea_create(login_screen);
    lv_obj_set_size(password_ta, 300, 40);
    lv_obj_align_to(password_ta,username_ta,LV_ALIGN_OUT_BOTTOM_MID, 0, 40);
    lv_obj_remove_style(password_ta, NULL, LV_PART_SCROLLBAR);
    lv_textarea_set_password_mode(password_ta, true);//设置密码模式，输入以*代替
    lv_textarea_set_placeholder_text(password_ta, "password");
    lv_obj_add_event_cb(password_ta, keyboard_event_cb, LV_EVENT_ALL,NULL);

    //  3.3创建user标签
    lv_obj_t *label_user = lv_label_create(login_screen);
    lv_label_set_text(label_user, "");
    lv_obj_set_style_text_font(label_user, &lv_font_montserrat_20, LV_PART_MAIN);
    lv_obj_align_to(label_user,username_ta, LV_ALIGN_OUT_RIGHT_MID,30,0);
    //  3.4创建password标签
    lv_obj_t *label_password = lv_label_create(login_screen);
    lv_label_set_text(label_password, "");
    lv_obj_set_style_text_font(label_password, &lv_font_montserrat_20, LV_PART_MAIN);
    lv_obj_align_to(label_password,password_ta, LV_ALIGN_OUT_RIGHT_MID,30,0);

    //  4.按钮:登录、注册
    // 登录
    lv_obj_t *Login_btn = lv_btn_create(login_screen);
    lv_obj_set_size(Login_btn,100,50);
    lv_obj_align_to(Login_btn,password_ta, LV_ALIGN_OUT_BOTTOM_MID,70,10);

    lv_obj_t *Login_label = lv_label_create(Login_btn);
    lv_obj_center(Login_label);
    lv_label_set_text(Login_label,"Login");
    lv_obj_set_style_text_font(Login_label,&lv_font_montserrat_20,0);


    seleteData *selete_data = malloc(sizeof(seleteData));
    if (selete_data == NULL) {
        printf("Memory allocation failed\n");
        return;
    }
    selete_data->username_ta = username_ta;
    selete_data->password_ta = password_ta;
    strcpy(selete_data->flag,buf);
    // printf("first flag =%s\n",selete_data->flag);
    lv_obj_add_event_cb(Login_btn,login_btn_event_cb,LV_EVENT_CLICKED,(void *)selete_data);
    if(strcmp(buf,"Admin login") != 0 )
    {
        //  注册
        lv_obj_t *Register_btn = lv_btn_create(login_screen);
        lv_obj_set_size(Register_btn,100,50);
        lv_obj_align_to(Register_btn,password_ta, LV_ALIGN_OUT_BOTTOM_MID,-70,10);

        lv_obj_t *Register_label = lv_label_create(Register_btn);
        lv_obj_center(Register_label);
        lv_label_set_text(Register_label,"Register");
        lv_obj_set_style_text_font(Register_label,&lv_font_montserrat_20,0);
        lv_obj_add_event_cb(Register_btn,register_ui_event_cb,LV_EVENT_CLICKED,NULL);
    }
    //键盘
    keyboard = lv_keyboard_create(lv_scr_act());
    lv_keyboard_set_textarea(keyboard, NULL);
    lv_obj_add_flag(keyboard, LV_OBJ_FLAG_HIDDEN);

    //5.退出
    lv_obj_t *quit_btn = lv_btn_create(login_screen);
    lv_obj_set_size(quit_btn,80,50);
    lv_obj_t *quit_label = lv_label_create(quit_btn);

    lv_obj_center(quit_label);
    lv_label_set_text(quit_label,LV_SYMBOL_LEFT);
    lv_obj_set_style_text_font(quit_label,&lv_font_montserrat_20,0);
    
    lv_obj_add_event_cb(quit_btn,return_origin_event_cb,LV_EVENT_CLICKED,NULL);
}


/**
 * 身份选择
 * 设置两个按钮:管理员身份登录 、用户登录
 * 对应相同的登录界面，不过登录后的界面不一致
*/
void Identity_choice(void)
{
    log_init();
    log_load_text("log.txt");
    /*-----1.创建身份选择屏幕-----*/
    lv_obj_t *Indentity_choice_screen = lv_obj_create(lv_scr_act());
    lv_obj_set_size(Indentity_choice_screen,800,480);
    lv_obj_set_style_border_width(Indentity_choice_screen, 0, 0);
    lv_obj_set_style_pad_all(Indentity_choice_screen, 0, 0);

    /*------2.背景图片------*/
    lv_obj_t *Indentity_bg_img = lv_img_create(Indentity_choice_screen);
    //    设置图片源
    LV_IMG_DECLARE(indentity_choice);
    lv_img_set_src(Indentity_bg_img,&indentity_choice);
    lv_obj_set_size(Indentity_bg_img,800,480);

    /*---------3.系统标签---------*/
    lv_obj_t *system_label = lv_label_create(Indentity_choice_screen);
    lv_label_set_text(system_label,"Smart commodity terminal");
    lv_obj_set_style_text_font(system_label,&lv_font_montserrat_48,0);
    lv_obj_align(system_label,LV_ALIGN_TOP_MID,0,10);
    lv_obj_set_style_text_color(system_label,lv_palette_main(LV_PALETTE_BLUE),0);
    
    /*--------4.身份按钮 管理员、用户----------*/
    //管理员按钮
    lv_obj_t *admin_login_btn = lv_btn_create(Indentity_choice_screen);
    lv_obj_set_size(admin_login_btn,200,100);
    lv_obj_set_style_bg_color(admin_login_btn,lv_palette_main(LV_PALETTE_GREY),0);
    lv_obj_align(admin_login_btn,LV_ALIGN_CENTER,-150,0);
    
    lv_obj_t *admin_login_label = lv_label_create(admin_login_btn);
    lv_label_set_text(admin_login_label,"Admin login");
    lv_obj_set_style_text_font(admin_login_label,&lv_font_montserrat_30,0);
    lv_obj_center(admin_login_label);

    lv_obj_add_event_cb(admin_login_btn,Login_interface_cb,LV_EVENT_CLICKED,NULL);

    //用户按钮
    lv_obj_t *user_login_btn = lv_btn_create(Indentity_choice_screen);
    lv_obj_set_size(user_login_btn,200,100);
    lv_obj_set_style_bg_color(user_login_btn,lv_palette_main(LV_PALETTE_GREY),0);
    lv_obj_align(user_login_btn,LV_ALIGN_CENTER,150,0);

    lv_obj_t *user_login_label = lv_label_create(user_login_btn);
    lv_label_set_text(user_login_label,"User login");
    lv_obj_set_style_text_font(user_login_label,&lv_font_montserrat_30,0);
    lv_obj_center(user_login_label);
    lv_obj_add_event_cb(user_login_btn,Login_interface_cb,LV_EVENT_CLICKED,NULL);

    //加载数据
    char user_data_str[256];
    snprintf(user_data_str, sizeof(user_data_str), "user.txt");
    if(user_list == NULL)
    {
        user_list= user_init_list();
        user_file_load_data(user_list,user_data_str);
    }

    //加载数据
    char goods_data_str[256];
    snprintf(goods_data_str, sizeof(goods_data_str), "goodsdata.txt");
    if(goods_list == NULL)
    {
        goods_list= init_list();
        file_load_data(goods_list,goods_data_str);
    }
}